/**
 * 应用配置文件
 * 管理API地址、环境设置等配置项
 */

import { ENV_CONFIG } from './env-config.js';

class AppConfig {
    constructor() {
        // 从环境配置获取基础配置
        this.environment = ENV_CONFIG.environment;
        this.apiConfig = this.getApiConfig();
        this.uiConfig = this.getUIConfig();
        this.debugConfig = this.getDebugConfig();
    }

    /**
     * 获取API配置
     */
    getApiConfig() {
        const configs = {
            development: {
                baseUrl: ENV_CONFIG.apiBaseUrl,
                timeout: 30000,
                retryAttempts: 3,
                retryDelay: 1000
            },
            staging: {
                baseUrl: ENV_CONFIG.apiBaseUrl,
                timeout: 30000,
                retryAttempts: 3,
                retryDelay: 1000
            },
            production: {
                baseUrl: ENV_CONFIG.apiBaseUrl,
                timeout: 30000,
                retryAttempts: 2,
                retryDelay: 2000
            }
        };

        return configs[this.environment] || configs.development;
    }

    /**
     * 获取UI配置
     */
    getUIConfig() {
        return {
            theme: 'dark',
            language: 'zh-CN',
            animations: {
                enabled: true,
                duration: 300
            },
            notifications: {
                duration: 3000,
                position: 'top-right'
            }
        };
    }

    /**
     * 获取调试配置
     */
    getDebugConfig() {
        const urlParams = new URLSearchParams(window.location.search);
        const debugParam = urlParams.get('debug');
        
        return {
            enabled: this.environment === 'development' || debugParam === 'true',
            logLevel: this.environment === 'development' ? 'debug' : 'info',
            showPerformanceMetrics: debugParam === 'true'
        };
    }

    /**
     * 获取完整的API URL
     */
    getApiUrl(endpoint = '') {
        return ENV_CONFIG.getApiUrl(endpoint);
    }

    /**
     * 获取音频文件URL
     */
    getAudioUrl(filename) {
        return ENV_CONFIG.getAudioUrl(filename);
    }

    /**
     * 检查是否为开发环境
     */
    isDevelopment() {
        return ENV_CONFIG.isDevelopment();
    }

    /**
     * 检查是否启用调试模式
     */
    isDebugEnabled() {
        return this.debugConfig.enabled;
    }

    /**
     * 更新配置
     */
    updateConfig(newConfig) {
        Object.assign(this, newConfig);
        this.saveToLocalStorage();
    }

    /**
     * 保存配置到本地存储
     */
    saveToLocalStorage() {
        try {
            const configToSave = {
                uiConfig: this.uiConfig,
                debugConfig: this.debugConfig
            };
            localStorage.setItem('appConfig', JSON.stringify(configToSave));
        } catch (error) {
            console.warn('无法保存配置到本地存储:', error);
        }
    }

    /**
     * 从本地存储加载配置
     */
    loadFromLocalStorage() {
        try {
            const savedConfig = localStorage.getItem('appConfig');
            if (savedConfig) {
                const config = JSON.parse(savedConfig);
                Object.assign(this.uiConfig, config.uiConfig || {});
                Object.assign(this.debugConfig, config.debugConfig || {});
            }
        } catch (error) {
            console.warn('无法从本地存储加载配置:', error);
        }
    }
}

// 创建全局配置实例
const appConfig = new AppConfig();

// 加载保存的配置
appConfig.loadFromLocalStorage();

// 导出配置实例
export { appConfig };
export default appConfig;